package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.user.entity.doctor.DoctorDetail;
import com.dd.cloud.user.entity.mechan.MechanRank;
import com.dd.cloud.user.req.mechan.*;
import com.dd.cloud.user.res.mechan.*;
import com.dd.cloud.user.service.mechan.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 机构账号表 前端控制器
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/mechan")
@Validated
@Slf4j
public class MechanController {
    @Autowired
    private IMechanUserService mechanUserService;
    @Autowired
    private IMechanRankService mechanRankService;
    @Autowired
    private IMechanReviewLogService mechanReviewLogService;
    @Autowired
    private IMechanRankApplyService mechanRankApplyService;

    /**
     * 机构管理员用户登录 验证码
     *
     * @param request
     * @param response
     * @param phone
     * @param code
     * @return
     */
    @PostMapping("/user/login/code")
    public ResponseEntity<MechanUserLoginRes> merchantUserLoginCode(HttpServletRequest request, HttpServletResponse response,
                                                                    @NotBlank(message = "手机号不能为空") String phone,
                                                                    @NotBlank(message = "验证码不能为空") String code) {
        MechanUserLoginRes res = mechanUserService.merchantUserLoginCode(request, response, phone, code);
        return ResponseEntity.ok(res);
    }

    /**
     * 机构管理员用户登录 密码
     *
     * @param request
     * @param response
     * @param account
     * @param password
     * @return
     */
    @PostMapping("/user/login/pwd")
    public ResponseEntity<MechanUserLoginRes> merchantUserLoginPwd(HttpServletRequest request, HttpServletResponse response,
                                                                   @NotBlank(message = "账号不能为空") String account,
                                                                   @NotBlank(message = "密码不能为空") String password) {
        MechanUserLoginRes res = mechanUserService.merchantUserLoginPwd(request, response, account, password);
        return ResponseEntity.ok(res);
    }

    /**
     * 机构管理员用户找回密码
     *
     * @param phone
     * @param code
     * @param pwd1
     * @param pwd2
     */
    @PostMapping("/user/find/pwd")
    public ResponseEntity<Void> findMerchantUserPwd(@NotBlank(message = "手机号不能为空") String phone,
                                                    @NotBlank(message = "验证码不能为空") String code,
                                                    @NotBlank(message = "新密码不能为空") String pwd1,
                                                    @NotBlank(message = "重复密码不能为空") String pwd2) {
        mechanUserService.findMerchantUserPwd(phone, code, pwd1, pwd2);
        return ResponseEntity.ok().build();
    }

    /**
     * 机构管理员用户修改密码
     *
     * @param oldPwd
     * @param newPwd1
     * @param newPwd2
     */
    @PostMapping("/user/update/pwd")
    public ResponseEntity<Void> updateMerchantUserPwd(@NotBlank(message = "原密码不能为空") String oldPwd,
                                                      @NotBlank(message = "新密码不能为空") String newPwd1,
                                                      @NotBlank(message = "重复密码不能为空") String newPwd2) {
        mechanUserService.updateMerchantUserPwd(oldPwd, newPwd1, newPwd2);
        return ResponseEntity.ok().build();
    }

    /**
     * 重置机构超管密码
     *
     * @param mechanId
     */
    @PostMapping("/user/pwd/reset")
    public ResponseEntity<Void> resetMerchantUserPwd(@NotNull(message = "机构id不能为空") Integer mechanId) {
        mechanUserService.resetAdminUserPwd(mechanId);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询机构详情
     *
     * @param mechanId 机构id
     * @return
     */
    @GetMapping("/rank/detail")
    public ResponseEntity<MechanRankDetailRes> queryMechanRankDetail(Integer mechanId) {
        MechanRankDetailRes res = mechanRankService.getMechanRankInfo(mechanId);
        return ResponseEntity.ok(res);
    }

    /**
     * 审核机构信息
     *
     * @param req
     */
    @PostMapping("/rank/review")
    public ResponseEntity<Void> reviewMechanRankInfo(@Valid @RequestBody MechanInfoReviewReq req) {
        mechanRankService.reviewMechanRankInfo(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询机构申请审核列表
     *
     * @param req
     * @return
     */
    @GetMapping("/rank/apply/page")
    public ResponseEntity<IPage<MechanRankApplyPageRes>> queryMechanRankApplyPage(@Valid MechanRankApplyPageReq req) {
        IPage<MechanRankApplyPageRes> res = mechanRankService.queryMechanRankApplyPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询机构审核记录列表
     *
     * @param req
     * @return
     */
    @GetMapping("/rank/review/page")
    public ResponseEntity<IPage<MechanRankReviewPageRes>> queryMechanRankReviewPage(@Valid MechanRankReviewPageReq req) {
        IPage<MechanRankReviewPageRes> res = mechanRankService.queryMechanRankReviewPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询机构申请记录的最后一条审核记录
     *
     * @param applyId
     * @return
     */
    @GetMapping("/rank/review/log")
    public ResponseEntity<MechanReviewLogRes> queryMechanReviewLogLast(@NotNull(message = "机构申请记录id不能为空") Integer applyId) {
        MechanReviewLogRes res = mechanReviewLogService.queryMechanReviewLogLast(applyId);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询机构管理员列表
     *
     * @param req
     * @return
     */
    @GetMapping("/user/page")
    public ResponseEntity<Page<MechanUserPage>> getMechanUserPage(@Valid MechanUserPageReq req) {
        Page<MechanUserPage> res = mechanUserService.getMechanUserPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询机构列表
     *
     * @param req
     * @return
     */
    @GetMapping("/rank/page")
    public ResponseEntity<Page<MechanRankPageRes>> getMechanRankPage(@Valid MechanRankPageReq req) {
        Page<MechanRankPageRes> res = mechanRankService.getMechanRankPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询机构列表 select框列表查询
     *
     * @param req
     * @return
     */
    @GetMapping("/rank/select")
    public ResponseEntity<List<MechanRankSelectRes>> getMechanRankSelect(@Valid MechanRankSelectReq req) {
        List<MechanRankSelectRes> res = mechanRankService.getMechanRankSelect(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 添加机构
     *
     * @param req
     * @return
     */
    @PostMapping("/rank/add")
    public ResponseEntity<MechanRank> addMechanRank(@Valid @RequestBody MechanRankAddReq req) {
        log.info("添加机构,{}", req);
        MechanRank rank = mechanRankService.addMechanRank(req);
        return ResponseEntity.ok(rank);
    }

    /**
     * excel添加机构
     *
     * @param file
     * @return
     */
    @PostMapping("/rank/add/excel")
    public ResponseEntity<MechanRank> addMechanRankByExcel(@NotNull(message = "请上传文件") MultipartFile file) {
        log.info("excel添加机构,{}", file);
        mechanRankService.addMechanRankByExcel(file);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改机构
     *
     * @param req
     * @return
     */
    @PostMapping("/rank/update")
    public ResponseEntity<Void> updateMechanRank(@Valid @RequestBody MechanRankUpdateReq req) {
        log.info("修改机构,{}", req);
        mechanRankService.updateMechanRank(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改机构状态
     *
     * @param mechanId
     * @return
     */
    @PostMapping("/rank/update/status")
    public ResponseEntity<Void> updateMechanRankStatus(@NotNull(message = "机构id不能为空") Integer mechanId,
                                                       @NotNull(message = "状态不能为空") Integer status,
                                                       HttpServletRequest request) {
        log.info("修改机构状态,{}", mechanId);
        mechanRankService.updateMechanRankStatus(mechanId, status, request);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改机构管理员
     *
     * @param req
     * @return
     */
    @PostMapping("/user/update")
    public ResponseEntity<Void> updateMechanUser(@Valid MechanUserUpdateReq req) {
        mechanUserService.updateMechanUser(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加机构管理员
     *
     * @param req
     * @return
     */
    @PostMapping("/user/add")
    public ResponseEntity<Void> addMechanUser(@Valid MechanUserAddReq req) {
        mechanUserService.addMechanUser(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改机构管理员状态
     *
     * @param id
     * @return
     */
    @PostMapping("/user/update/status")
    public ResponseEntity<Void> updateMechanUserStatus(@NotNull(message = "管理员id不能为空") Integer id,
                                                       @NotNull(message = "状态不能为空") Integer status) {
        mechanUserService.updateMechanUserStatus(id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取机构下的医生名单
     */
    @GetMapping("/doctor/list")
    public ResponseEntity<List<DoctorDetail>> getDoctorList(@RequestParam("mechanId") Integer mechanId) {
        List<DoctorDetail> doctorUserList = mechanRankService.getDoctorList(mechanId);
        return ResponseEntity.ok(doctorUserList);
    }

    /**
     * 迁移所有医生到指定机构
     */
    @PostMapping("/move/doctor")
    public ResponseEntity<Void> moveDoctorToMechanRank(@Valid @RequestBody DoctorToMechanReq doctorToMechanReq) {
        mechanRankService.moveDoctorToMechanRank(doctorToMechanReq);
        return ResponseEntity.ok().build();
    }


    /**
     * 查询机构申请详情
     *
     * @param applyId
     * @return
     */
    @GetMapping("/rank/review/detail")
    public ResponseEntity<MechanRankApplyDetailRes> queryMechanReviewDetail(@NotNull(message = "机构申请记录id不能为空") Integer applyId) {
        MechanRankApplyDetailRes res = mechanReviewLogService.queryMechanReviewDetail(applyId);
        return ResponseEntity.ok(res);
    }


    /**
     * 机构注册申请
     */
    @PostMapping("/rank/detail/apply")
    public ResponseEntity<Void> insertRankApply(@Valid @RequestBody MechanRankApplyAddReq mechanRankApplyAddReq) {
        mechanRankService.insertRankApply(mechanRankApplyAddReq);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询注销列表
     *
     * @param req
     * @return
     */
    @GetMapping("/rank/remove/page")
    public ResponseEntity<IPage<MechanRankPageRes>> removeMechanRankPage(@Valid MechanRankPageReq req) {
        IPage<MechanRankPageRes>  res = mechanRankService.removeMechanRankPage(req);
        return ResponseEntity.ok(res);
    }

}

